package com.zyx.spider.site.eastmoney;

import lombok.extern.slf4j.Slf4j;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

@Slf4j
@Component
@ConditionalOnProperty(name = "spider.site.eastmoney.enabled", havingValue = "true")
public class EMNewsCrawlerTask {
    // 示例新闻网站URL
    private static final String NEWS_URL = "https://finance.eastmoney.com/a/cgnjj.html";
    // 模拟浏览器UA
    private static final String USER_AGENT = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36";

    /**
     * 每30分钟执行一次新闻抓取
     */
    @Scheduled(fixedRate = 300, initialDelay = 5, timeUnit = TimeUnit.SECONDS)
    public void crawlNews() {
        log.info("开始执行【东方财富网】新闻抓取任务...");

        try {
            // 创建链接
            Connection conn = Jsoup.connect(NEWS_URL)
                    .timeout(5000) // 设置超时时间（毫秒）
                    .userAgent(USER_AGENT) // 模拟浏览器
                    .ignoreHttpErrors(true); // 忽略 HTTP 错误（如 404）;

            // 获取响应状态码
            Connection.Response response = conn.execute();
            int statusCode = response.statusCode();

            if (statusCode == 200) {
                // 状态码正常，解析文档
                Document document = response.parse();

                // 检查标题
                if (document.title().isEmpty()) {
                    log.warn("警告：页面标题为空");
                    return;
                }else {
                    log.info("抓取到新闻栏目标题--「{}」", document.title());
                }

                // 抓取新闻列表外层元素
                Element newsList = document.selectFirst("ul#newsListContent");
                if (newsList == null || !newsList.hasText()) {
                    log.warn("未找到[新闻列表]元素");
                    return;
                }

                // 获取新闻列表
                Elements items = newsList.select("li");
                if (items.isEmpty()) {
                    log.warn("未找到[新闻]元素");
                    return;
                }

                // 遍历新闻列表
                for (org.jsoup.nodes.Element item : items) {
                    // 获取新闻链接
                    Element link = Objects.requireNonNull(item.selectFirst("p.title > a"));
                    // 获取新闻标题
                    Element headline = Objects.requireNonNull(item.selectFirst("p.title"));
                    // 获取新闻简介
                    Element summary = Objects.requireNonNull(item.selectFirst("p.info"));

                    log.info("《{}》-- {} 「{}」", headline.text(), link.attr("href"), summary.text());
                }

            } else {
                log.warn("请求失败，状态码：{}", statusCode);
            }
        } catch (IOException e) {
            log.error("网络异常：{}", e.getMessage());
        }
    }
}
